←Select platform

ReplaceIntersectedCharacters(IEnumerable<LeadRect>,char) Method

Summary

Replaces the characters intersecting with the specified rectangles.

Syntax
C#
VB
C++
public bool ReplaceIntersectedCharacters( 
   IEnumerable<LeadRect> bounds, 
   char replaceCharacter 
) 
Public Function ReplaceIntersectedCharacters( 
   ByVal bounds As IEnumerable(Of LeadRect 
), 
   ByVal replaceCharacter As Char) As Boolean 
public:  
   bool ReplaceIntersectedCharacters( 
      IEnumerable<LeadRect^>^ bounds, 
      Char replaceCharacter 
   ) 

Parameters

bounds

List of rectangles containing the bounds to check. These values are in pixels.

replaceCharacter

The new character code to use. Use 0 to delete the characters.

Return Value

true if one or more characters were replaced; otherwise, false.

Remarks

This is a helper method used to quickly perform actions such as redacting or removing characters from the recognition data based on their locations.

This method calls ReplaceIntersectedCharacters(rects, replaceCharacter, intersectionPercentage) with intersectionPercentage equal to 0.

Example

The following example performs OCR, then redacts all characters under a specified rectangle, and saves the result as a PDF file.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Common; 
using Leadtools.Document.Writer; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
public void OcrAndRedactToPdf() 
{ 
   string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "OCR1.tif"); 
   string pdfFileNameBefore = Path.Combine(LEAD_VARS.ImagesDir, "OCR-Redact-Before.pdf"); 
   string pdfFileNameAfter = Path.Combine(LEAD_VARS.ImagesDir, "OCR-Redact-After.pdf"); 
 
   // Create OCR engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false)) 
   { 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // Load the OCR1.tif image and recognize it 
      RasterImage image = ocrEngine.RasterCodecsInstance.Load(tifFileName, 1); 
      using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose)) 
      { 
         ocrPage.Recognize(null); 
 
         // Save it, note that all the data is in the result PDF file, including "Fast Facts" 
         using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile)) 
         { 
            ocrDocument.Pages.Add(ocrPage); 
            ocrDocument.Save(pdfFileNameBefore, DocumentFormat.Pdf, null); 
         } 
 
         // Now, redact "Fast Facts", this could be an input from the user based on rubber banding or Regex. 
         LeadRect redactRect = LeadRect.FromLTRB(300, 800, 730, 900); 
         char replaceCharacter = '*'; 
         IOcrPageCharacters ocrPageCharacters = ocrPage.GetRecognizedCharacters(); 
         ocrPageCharacters.ReplaceIntersectedCharacters(new LeadRect[] { redactRect }, replaceCharacter); 
         ocrPage.SetRecognizedCharacters(ocrPageCharacters); 
 
         // Save it again, note that "Fast Facts" is replaced with * characters 
         using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile)) 
         { 
            ocrDocument.Pages.Add(ocrPage); 
            ocrDocument.Save(pdfFileNameAfter, DocumentFormat.Pdf, null); 
         } 
      } 
   } 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Ocr 
Imports Leadtools.Forms 
Imports Leadtools.Document.Writer 
Imports Leadtools.WinForms 
Imports Leadtools.Drawing 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
 
Public Sub OcrAndRedactToPdf() 
   Dim tifFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "OCR1.tif") 
   Dim pdfFileNameBefore As String = Path.Combine(LEAD_VARS.ImagesDir, "OCR-Redact-Before.pdf") 
   Dim pdfFileNameAfter As String = Path.Combine(LEAD_VARS.ImagesDir, "OCR-Redact-After.pdf") 
 
   ' Create OCR engine 
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, False) 
      ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrLEADRuntimeDir) 
 
      ' Load the OCR1.tif image And recognize it 
      Dim Image As RasterImage = ocrEngine.RasterCodecsInstance.Load(tifFileName, 1) 
      Using ocrPage As IOcrPage = ocrEngine.CreatePage(Image, OcrImageSharingMode.AutoDispose) 
         ocrPage.Recognize(Nothing) 
 
         ' Save it, note that all the data Is in the result PDF file, including "Fast Facts" 
         Using ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument(Nothing, OcrCreateDocumentOptions.AutoDeleteFile) 
            ocrDocument.Pages.Add(ocrPage) 
            ocrDocument.Save(pdfFileNameBefore, DocumentFormat.Pdf, Nothing) 
         End Using 
 
         ' Now, redact "Fast Facts", this could be an input from the user based on rubber banding Or Regex. 
         Dim redactRect As LeadRect = LeadRect.FromLTRB(300, 800, 730, 900) 
         Dim replaceCharacter As Char = "*"c 
         Dim ocrPageCharacters As IOcrPageCharacters = ocrPage.GetRecognizedCharacters() 
         ocrPageCharacters.ReplaceIntersectedCharacters(New LeadRect() {redactRect}, replaceCharacter) 
         ocrPage.SetRecognizedCharacters(ocrPageCharacters) 
 
         ' Save it again, note that "Fast Facts" Is replaced with * characters 
         Using ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument(Nothing, OcrCreateDocumentOptions.AutoDeleteFile) 
            ocrDocument.Pages.Add(ocrPage) 
            ocrDocument.Save(pdfFileNameAfter, DocumentFormat.Pdf, Nothing) 
         End Using 
      End Using 
   End Using 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
   Public Const OcrLEADRuntimeDir As String = "C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime" 
End Class 

Requirements

Target Platforms

Help Version 20.0.2020.4.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Ocr Assembly